<template>
	<view class="index-container">
		<!-- 轮播图 -->
		<swiper class="banner" indicator-dots autoplay circular interval="3000">
			<swiper-item v-for="(item, index) in bannerList" :key="index">
				<image :src="item.image" class="banner-image" @click="goToBanner(item)"></image>
			</swiper-item>
		</swiper>
		
		<!-- 导航分类 -->
		<view class="nav-section">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="goToCategory(item)">
				<image :src="item.icon" class="nav-icon"></image>
				<text class="nav-text">{{ item.name }}</text>
			</view>
		</view>
		
		<!-- 推荐商品 -->
		<view class="recommend-section">
			<view class="section-header">
				<text class="section-title">推荐商品</text>
			</view>
			<view class="product-list">
				<view class="product-item" v-for="(item, index) in recommendList" :key="index">
					<view class="product-left" @click="goToDetail(item)">
						<image :src="item.image" class="product-image"></image>
					</view>
					<view class="product-middle" @click="goToDetail(item)">
						<view class="product-name">{{ item.name }}</view>
						<view class="product-price-section">
							<text class="product-original-price">¥{{ item.originalPrice }}</text>
							<text class="product-discount-price">¥{{ item.discountPrice }}/{{ item.unit }}</text>
						</view>
					</view>
					<view class="product-right">
						<view class="product-sold">已售{{ item.soldCount }}件</view>
						<view v-if="getItemQuantity(item.id) === 0" class="add-cart-btn" @click="addToCart(item)">加入购物车</view>
						<view v-else class="quantity-control">
							<view class="quantity-btn" @click="decreaseQuantity(item)">-</view>
							<view class="quantity-num">{{ getItemQuantity(item.id) }}</view>
							<view class="quantity-btn" @click="increaseQuantity(item)">+</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapActions } from 'vuex'
	import auth from '@/utils/auth.js'
	
	export default {
		data() {
			return {
				bannerList: [
					{
						id: 1,
						image: 'https://qcloud.dpfile.com/pc/bRRACor_pnM43qVHABanmaWvkr3Z0OE1xv_gPeb1FEkk0Q0599ShPbm4SJMyRbzk.jpg',
						url: '/pages/activity/seckill'
					},
					{
						id: 2,
						image: 'https://qcloud.dpfile.com/pc/6ik4az2ITMd4FdExn7wXyk_hugX_O1DkWEa5wFRChBFPDfG_Dp9RFL4aQ-qWBys3.jpg',
						url: '/pages/activity/new'
					},
					{
						id: 3,
						image: 'https://hellorfimg.zcool.cn/provider_image/large/2236981854.jpg',
						url: '/pages/activity/discount'
					}
				],
				navList: [
					{
						id: 1,
						name: '猪肉丸',
						icon: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Fi4%2F1660314386%2FO1CN011FyzpL1iGppwfxqQY_%21%211660314386.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1763471848&t=dc4b7df2417a3d839dadacbc389e9cd5',
						url: '/pages/product/list?type=new'
					},
					{
						id: 2,
						name: '仙人板',
						icon: 'https://img14.360buyimg.com/pop/jfs/t1/158199/35/20921/93931/607fced3E9509afc0/f40fbed1105b70a9.jpg',
						url: '/pages/activity/seckill'
					},
					{
						id: 3,
						name: '肉肠',
						icon: 'https://img0.baidu.com/it/u=3952203451,478744648&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500',
						url: '/pages/product/list?type=discount'
					},
					{
						id: 4,
						name: '猪肉干',
						icon: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Fbao%2Fuploaded%2FTB19uqsUwHqK1RjSZFPSuwwapXa.jpg_310x310.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1763473392&t=b660bfa175cf46756194797db6c42381',
						url: '/pages/category/category'
					}
				],
				recommendList: [
					{
						id: 201,
						name: '兴宁手打猪肉丸',
						originalPrice: 5999,
						discountPrice: 4999,
						soldCount: 128,
						count: 0,
						unit: '斤',
						image: 'https://pic.rmb.bdstatic.com/bjh/video/46cace1e2cb2827fc520fbc6769ebef5.jpeg'
					},
					{
						id: 202,
						name: '自家做的仙人板',
						originalPrice: 149,
						discountPrice: 99,
						soldCount: 256,
						count: 0,
						unit: '碗',
						image: 'https://qcloud.dpfile.com/pc/Z1XbyrAyCWz1qqscp3gTNrC2EIqbevu6XaPpnfpH4cyOy49x-iCOD4mvVJcr1l6I.jpg'
					},
					{
						id: 203,
						name: '自家晒的猪肉干',
						originalPrice: 399,
						discountPrice: 299,
						soldCount: 89,
						count: 0,
						unit: '斤',
						image: 'https://pic.rmb.bdstatic.com/bjh/241216/dump/279b49f325fde86015838cea70e4db79.jpeg'
					},
					{
						id: 204,
						name: '自家做的肉肠',
						originalPrice: 499,
						discountPrice: 399,
						soldCount: 67,
						count: 0,
						unit: '斤',
						image: 'https://img0.baidu.com/it/u=3364154920,628749022&fm=253&app=138&f=JPEG?w=500&h=500'
					}
				]
			}

		},
		computed: {
			// 映射购物车相关的状态和getters
			...mapState('cart', ['cartItems']),
			...mapGetters('cart', ['getItemQuantity'])
		},
		onLoad() {
			
		},
		onShow() {
			// 每次显示页面时刷新数据
			this.loadIndexData()
			this.initCart()
		},
		methods: {
			// 映射购物车相关的actions
			...mapActions('cart', ['addToCart', 'updateQuantity', 'removeFromCart', 'initCart']),
			// 加载首页数据
			loadIndexData() {
				// 这里可以调用API获取首页数据
				console.log('加载首页数据')
			},
			
			// 点击轮播图
			goToBanner(item) {
				if (item.url) {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			
			// 跳转到分类或活动页
			goToCategory(item) {
				if (item.url) {
					// 如果是tabBar页面，使用switchTab
					if (item.url === '/pages/category/category') {
						uni.switchTab({
							url: item.url
						})
					} else {
						uni.navigateTo({
							url: item.url
						})
					}
				}
			},
			
			// 跳转到商品详情
			goToDetail(item) {
				uni.navigateTo({
					url: `/pages/product/detail?id=${item.id}`
				})
			},
			
			// 添加到购物车
			addToCart(item) {
				// 检查登录状态
				if (!auth.checkLoginAndRedirect(auth.getCurrentPageFullPath())) {
					return
				}
				
				this.$store.dispatch('cart/addToCart', {
					id: item.id,
					name: item.name,
					price: item.discountPrice,
					image: item.image,
					quantity: 1
				})
				uni.showToast({
					title: '已添加到购物车',
					icon: 'success'
				})
			},
			
			// 增加数量
			increaseQuantity(item) {
				// 检查登录状态
				if (!auth.checkLoginAndRedirect(auth.getCurrentPageFullPath())) {
					return
				}
				
				this.updateQuantity({
					id: item.id,
					quantity: this.getItemQuantity(item.id) + 1
				})
			},
			
			// 减少数量
			decreaseQuantity(item) {
				const currentQuantity = this.getItemQuantity(item.id)
				if (currentQuantity > 1) {
					this.updateQuantity({
						id: item.id,
						quantity: currentQuantity - 1
					})
				} else if (currentQuantity === 1) {
					// 如果数量为1，则从购物车中移除
					this.removeFromCart({
						id: item.id
					})
				}
			},
			
			// 跳转到订单详情（测试路由守卫）
			goToOrderDetail() {
				uni.navigateTo({
					url: '/pages/order/detail?id=123'
				})
			}
		}
	}
</script>

<style>
.index-container {
	background-color: #f5f5f5;
}

/* 轮播图 */
.banner {
	height: 400rpx;
}

.banner-image {
	width: 100%;
	height: 100%;
}

/* 导航分类 */
.nav-section {
	display: flex;
	justify-content: space-around;
	padding: 30rpx 0;
	background-color: #fff;
	margin: 20rpx 0;
}

.nav-item {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.nav-icon {
	width: 80rpx;
	height: 80rpx;
	margin-bottom: 10rpx;
}

.nav-text {
	font-size: 28rpx;
	color: #333;
}

/* 推荐商品 */
.recommend-section {
	background-color: #fff;
	margin: 20rpx;
	padding: 20rpx;
	border-radius: 10rpx;
}

.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.product-list {
	display: flex;
	flex-direction: column;
}

.product-item {
	display: flex;
	align-items: center;
	padding: 30rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
}

.product-item:last-child {
	border-bottom: none;
}

.product-left {
	flex: 0 0 200rpx;
	margin-right: 20rpx;
}

.product-image {
	width: 200rpx;
	height: 200rpx;
	border-radius: 10rpx;
}

.product-middle {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.product-right {
	flex: 0 0 180rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.product-name {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.product-price-section {
	margin-bottom: 10rpx;
}

.product-original-price {
	font-size: 24rpx;
	color: #999;
	text-decoration: line-through;
	margin-right: 10rpx;
}

.product-discount-price {
	font-size: 32rpx;
	color: #e93b3d;
	font-weight: bold;
}

.product-sold {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 15rpx;
}

.add-cart-btn {
	background-color: #e93b3d;
	color: #fff;
	border: none;
	border-radius: 30rpx;
	padding: 15rpx 25rpx;
	font-size: 24rpx;
	line-height: 1.5;
	text-align: center;
}

.quantity-control {
	display: flex;
	align-items: center;
	justify-content: space-between;
	width: 150rpx;
}

.quantity-btn {
	width: 45rpx;
	height: 45rpx;
	border-radius: 50%;
	background-color: #e93b3d;
	color: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	font-weight: bold;
}

.quantity-num {
	font-size: 30rpx;
	color: #333;
	min-width: 40rpx;
	text-align: center;
}
</style>
